﻿var IDMark_A = "_a"; //860000732
function datechange() {
    var r = confirm("是否确认修改!");
    if (r == true) {
        return true;
    } else {
        return false;
    }
}

function showEditDate(btn1, btn2, btn3) {
    $("#" + btn2).show();
    $("#" + btn3).show();
    $("#" + btn1).hide();
}

function _saveDate(btn1, btn2, btn3, datetype, selfid, oldate) {
    var newdate = $("#" + btn2).val();

    $.ajax({
        type: "post",
        url: "/ashx/tree.ashx",
        data: { date: newdate, type: datetype, id: selfid, oldate: oldate },
        dataType: "text",
        success: function (msg) {
            if (msg == "1") {
                $("#" + btn1).html(newdate);
                $("#" + btn1).show();
                $("#" + btn2).hide();
                $("#" + btn3).hide();
            }
            else {
                alert('修改不成功');
            }
        }
    });
}

function date(strdate, nodeid, datetype, dateid, bardate) {
    var strhtml = "";
    var arrdate = strhtml.split('|');
    var i = 0;
    var labdateid = "";
    var editdateid = "";
    var btndateid = "";
    $.each(arrdate, function (key, val) {
        if (val != null && val != "") {
            if (i > 0) {
                strhtml += "<p><br/><p>";
            }
            labdateid = "labdate_" + nodeid + "_" + i;
            editdateid = "editdate_" + nodeid + "_" + i;
            btndateid = "btndate_" + nodeid + "_" + i;
            if (datetype == "3") {
                editStr = editStr + "<input id='" + btndateid + "' type='button' onclick=\"_saveDate('" + labdateid + "','" + editdateid + "','" + btndateid + "'," + datetype + "," + dateid + ",'" + val + "')\" value='保存'  style='display:none;'/> ";
                editStr = editStr + "<input id='" + btndateid + "' type='button' onclick=\"_saveDate('" + labdateid + "','" + editdateid + "','" + btndateid + "'," + datetype + "," + dateid + ",'" + val + "')\" value='保存'  style='display:none;'/> ";
                editStr = editStr + "<input id='" + btndateid + "' type='button' onclick=\"_saveDate('" + labdateid + "','" + editdateid + "','" + btndateid + "'," + datetype + "," + dateid + ",'" + val + "')\" value='保存'  style='display:none;'/> ";

            }

            editStr = editStr + "<span id='" + labdateid + "' onclick=\"showEditDate('" + labdateid + "','" + editdateid + "','" + btndateid + "')\">" + val + "</span>";
            editStr = editStr + "<input id='" + editdateid + "' value='" + val + "' style='display:none;'/>";
            editStr = editStr + "<input id='" + btndateid + "' type='button' onclick=\"_saveDate('" + labdateid + "','" + editdateid + "','" + btndateid + "'," + datetype + "," + dateid + ",'" + val + "')\" value='保存'  style='display:none;'/> ";
            editStr = editStr + "</div>";

            i = i + 1;
        }

        //回调函数有两个参数,第一个是元素索引,第二个为当前值
        //alert('fruit数组中，索引：' + key + '对应的值为：' + val);
    });

    //        $(".date1").datepicker({
    //            "dateFormat": "yyyy-mm-dd"
    //        });
    return strhtml;
}


function addDiyDom(treeId, treeNode) {
    var aObj = $("#" + treeNode.tId + IDMark_A);
    var diyBtn2 = "diyBtn2_" + treeNode.id;
    var diyBtn3 = "diyBtn3_" + treeNode.id;
    var diyBtn4 = "diyBtn4_" + treeNode.id;
    var editStr = "<a id='diyBtn1_" + treeNode.id + "' href='" + treeNode.url + "'>" + treeNode.selfno + " &nbsp;&nbsp;" + treeNode.nwt + "</a>";
    editStr = editStr + "<div style='padding-left:40px;'>";

    //date
    var datetype = treeNode.datetype;
    if (datetype == "3") {
        datetype = "5";
        var strdate = treeNode.bardate;
        var arr = strdate.split(",");
        for (i = 0; i < arr.length; i++) {
            diyBtn2 = "diyBtn2_" + i.toString() + "_" + treeNode.id;
            diyBtn3 = "diyBtn3_" + i.toString() + "_" + treeNode.id;
            diyBtn4 = "diyBtn4_" + i.toString() + "_" + treeNode.id;
            editStr = editStr + "<span id='diyBtn2_" + i.toString() + "_" + treeNode.id + "' onclick=\"showEditDate('" + diyBtn2 + "','" + diyBtn3 + "','" + diyBtn4 + "')\">" + arr[i] + "</span>";
            editStr = editStr + "<input id='diyBtn3_" + i.toString() + "_" + treeNode.id + "' value='" + arr[i] + "' style='display:none;'/>";
            editStr = editStr + "<input id='diyBtn4_" + i.toString() + "_" + treeNode.id + "' type='button' onclick=\"_saveDate('" + diyBtn2 + "','" + diyBtn3 + "','" + diyBtn4 + "'," + datetype + "," + treeNode.selfid + ",'" + arr[i] + "')\" value='保存'  style='display:none;'/> ";
            editStr = editStr + " <br/> ";
        }
    }
    else {
        diyBtn2 = "diyBtn2_" + treeNode.id;
        diyBtn3 = "diyBtn3_" + treeNode.id;
        diyBtn4 = "diyBtn4_" + treeNode.id;

        //alert(treeNode.bardate);
        editStr = editStr + "<span id='diyBtn2_" + treeNode.id + "' onclick=\"showEditDate('" + diyBtn2 + "','" + diyBtn3 + "','" + diyBtn4 + "')\">" + treeNode.createtime + "</span>";
        editStr = editStr + "<input id='diyBtn3_" + treeNode.id + "' value='" + treeNode.createtime + "' style='display:none;'/>";
        editStr = editStr + "<input id='diyBtn4_" + treeNode.id + "' type='button' onclick=\"_saveDate('" + diyBtn2 + "','" + diyBtn3 + "','" + diyBtn4 + "'," + treeNode.datetype + "," + treeNode.selfid + ",'" + treeNode.createtime + "')\" value='保存'  style='display:none;'/> ";
    }
    editStr = editStr + "</div>";
    aObj.after(editStr);
}

function searchclick() {
    $("#searchDiv").dialog("close");

    var chval = $("input[name=raono]:checked").val();
    var txtno = "";
    if (chval != null) {
        txtno = $("input[name=raono]:checked").next().val();
    }
    else {
        alert('请选择查询单号');
        return;
    }

    if ($("#txtSearcheTxt").val() != "") {
        $.ajax({
            type: "post",
            url: "/ashx/tree.ashx",
            data: { no: txtno, notype: chval },
            dataType: "json",
            success: function (msg) {
                if (msg == "")
                    alert('没有数据');

                var zNodes = msg;

                //var zNodes = [{ "id": 1,"parid":"55", "name": "860000001", children: [{ "id": 2, "name": "860000001001", children: [{ "id": 3, "name": "M0000538", children: [{ "id": 4, "name": "F0000331", children: [{ "id": 5, "name": "RF000360"}]}]}]}]}];

                var setting = {
                    view: {
                        addDiyDom: addDiyDom
                    }
                };

                $.fn.zTree.init($("#treeDemo"), setting, zNodes);

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
    }
}

$("input[name=raono]").change(function () {
    $(".txtno").attr("disabled", "disabled");
    if ($(this).attr("checked")) {
        $(this).next().removeAttr("disabled");
    }
});

$("#searchDiv").dialog({
    width: 368,
    height: 180,
    resizable: false,
    position: 'center',
    dialogClass: "noClose",
    modal: true
});